﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MAPDavy.Entities;
using System.Diagnostics;

namespace MAPDavy.Repository
{
    public class GradeRepository: MasterRepository,IGradeRepository
    {
        public List<Grade> GetAllFromGrade()
        {
            var query = from c in Context.Grades
                        select c;

            return query.ToList();
        }


        public List<Grade> GetFromGradeByTermId(int termId)
        {
            var query = (from g in Context.Grades.Include("Seccions") 
                        join s in Context.Seccions on g.Id equals s.GradeId
                        join st in Context.SeccionTutors on s.Id equals st.SeccionId
                        join tg in Context.TestGrades on s.GradeId equals  tg.GradeId
                        where tg.Anio == st.Anio && tg.TermId == termId 
                        select g).Distinct();

            //EventLog _log = new EventLog();

            //if (!EventLog.SourceExists("MAPDavy01"))
            //{
            //    EventLog.CreateEventSource("MAPDavy01",
            //    Environment.UserName.ToString());
            //}

            //foreach (var item in query.ToList())
            //{
            //    _log.Source = "MAPDavy";
            //    _log.WriteEntry("Error : " + item.Id.ToString() + " - " + item.GradeLevel, EventLogEntryType.Warning);
            //    _log.Dispose();
            //} 

            return query.ToList();
        }
    }
}
